package com.school.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.school.admin.common.annotation.DataAuthority;
import com.school.admin.common.annotation.IgnoreSecurity;
import com.school.admin.common.handler.GlobalException;
import com.school.admin.common.result.BaseResult;
import com.school.admin.common.result.R;
import com.school.admin.common.utils.*;
import com.school.admin.entity.MallSysUser;
import com.school.admin.entity.vo.MallSysUserQueryVo;
import com.school.admin.entity.vo.MallSysUserVo;
import com.school.admin.service.MallSysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author sunpx
 * @since 2022-03-24
 */
@RestController
@RequestMapping("/mall-sys-user")
@Api(tags = "用户相关")
public class MallSysUserController extends BaseController {

    @Autowired
    private MallSysUserService mallSysUserService;


    /**
     * 登录接口
     */
    @PostMapping("login")
    @IgnoreSecurity
    @ApiOperation("登录")
    @DataAuthority(name = "登录",isAuthorize = false ,key = 9)
    public R login(@RequestBody MallSysUser user, HttpServletRequest request) {

        Map<Object, Object> sysParameterUtil = getSysParameterUtil();
        //获取用户登录过期数据
        String singFailTime = String.valueOf(sysParameterUtil.get(BaseResult.DicTypeResult.SYS_PARAMETER_SING_FAIL_TIME));
        singFailTime= "null".equals(singFailTime)?"1800000":singFailTime;
        val login = mallSysUserService.login(user, request, Long.valueOf(singFailTime));
        // 获取用户基本信息
        MallSysUser userInfo = (MallSysUser) login.get("userInfo");

        return R.ok().data(login);
    }




    /**
     * @return : top.zzzit.main.matter.common.result.R
     * @author : zzz
     * @desc : 分页查询
     * @date :  14:54:24
     * @params : commonQuery
     */
    @PostMapping("queryPage")
    @ApiOperation("查看用户列表")
    public R queryPage(@RequestBody MallSysUserQueryVo mallSysUserQueryVo) {
        return R.ok().data("list", mallSysUserService.queryPage(mallSysUserQueryVo));
    }


    /**
     * @return : top.zzzit.main.matter.common.result.R
     * @author : zzz
     * @desc : 更新用户状态
     * @date :  14:53:58
     * @params : uid,b
     */
    @PutMapping("/state")
    @ApiOperation("更新用户状态")
    public R uptStatus(@RequestBody MallSysUser mallSysUser) {
        mallSysUserService.uptUser(mallSysUser);
        return R.ok();
    }

    @PostMapping("save")
    @ApiOperation("添加用户")
    @Transactional
    public R saveUser(@RequestBody MallSysUserVo mallSysUser) {

        QueryWrapper<MallSysUser> mallSysUserQueryWrapper = new QueryWrapper<>();
        mallSysUserQueryWrapper
                .eq("username", mallSysUser.getUsername());
        MallSysUser one = mallSysUserService.getOne(mallSysUserQueryWrapper);
        if (StringUtils.isEmpty(mallSysUser.getId())) {
            if (one != null) {
                throw new GlobalException(50001, "用户名重复");
            }
            mallSysUser.setUserNumber(SerialNumberUtil.serial("ADMIN", "yyyyMMddHHmmss", 20, null));


            mallSysUserService.addUser(mallSysUser);
        } else {
            mallSysUserService.uptUserInfo(mallSysUser, one);
        }

        return R.ok();
    }




    @DeleteMapping("del/{id}")
    @ApiOperation("删除用户")
    public R delUser(@PathVariable String id) {
        boolean b = mallSysUserService.delUser(id);
        return R.th(b);
    }


    @GetMapping("getById/{id}")
    @ApiOperation("查询用户详情")
    public R getByIdUser(@PathVariable String id) {
        return R.ok().data(mallSysUserService.userDetail(id));
    }

    @GetMapping("getByIdThis")
    @ApiOperation("查询用户详情")
    public R getByIdUserThis() {
        return R.ok().data(mallSysUserService.userDetail(UserInfoUtil.getUserId()));
    }

    /**
     * 根据id删除商品图片表信息
     *
     * @author zzz
     * @since 2021-01-12
     */
    @PostMapping("uploadMallGoodsImg")
    @ApiOperation("上传图片")
    public R uploadImg(MultipartFile httpFile) throws Exception {
        FieldAssert.isNull(httpFile, "请传入图片");
        return R.ok().data("path", FileUtil.uploadAffix(httpFile, "userImg", UserInfoUtil.getUserId()));

    }

    @PostMapping("uploadAvatar")
    @ApiOperation("保存头像")
    public R uploadAvatar(@RequestBody MallSysUser mallSysUser) {
        UpdateWrapper<MallSysUser> mallSysUserQueryWrapper = new UpdateWrapper<>();
        mallSysUserQueryWrapper.eq("id", UserInfoUtil.getUserId()).set("img_url", mallSysUser.getImgUrl());
        mallSysUserService.update(mallSysUserQueryWrapper);
        return R.ok();
    }


}

